package shiyan;

public class Complex {
    // 定义属性并生成getter,setter
    private double RealPart;
    private double ImagePart;
    // 定义构造函数
//    public Complex()
    public Complex(double RealPart, double ImagePart){
        this.RealPart = RealPart;
        this.ImagePart = ImagePart;

    }
    public double getRealPart(double RealPart){
        return RealPart;
    }
    //Override Object
    public boolean equals(Object obj){
        if(this==obj)
            return (this == obj);
        if(obj==null)
            return false;
        return true;
    }
    public String toString() {
        String s = " ";
        if (ImagePart > 0)
            s =  RealPart + "+" + ImagePart + "i";
        if (ImagePart == 0)
            s =  RealPart + "";
        if (ImagePart < 0)
            s = RealPart + " " + ImagePart + "i";
        return s;
    }

    // 定义公有方法:加减乘除
    public Complex ComplexAdd(Complex a){
        return  new Complex(RealPart+a.RealPart,ImagePart+a.ImagePart);
    }//自相加
    public Complex ComplexSub(Complex a){
        return new Complex(RealPart-a.RealPart,ImagePart-a.ImagePart);
    }
    public Complex ComplexMulti(Complex a){
        return new Complex(RealPart*a.RealPart,ImagePart*a.ImagePart);
    }
    public Complex ComplexDiv(Complex a){
        if(RealPart==0||ImagePart==0||a.ImagePart==0||a.RealPart==0)
            return new Complex(0.0,0.0);
        double d = Math.sqrt(a.RealPart*a.RealPart)+Math.sqrt(a.ImagePart*a.ImagePart);
        return new Complex((RealPart*a.RealPart+ImagePart*a.ImagePart)/d,Math.round((RealPart*a.ImagePart-ImagePart*a.RealPart)/d));
    }



}
